
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.domain.Region;
import me.zhengjie.modules.system.domain.vo.RegionQueryCriteria;
import me.zhengjie.modules.system.service.RegionService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author 程康
* @date 2024-12-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "地区管理管理")
@RequestMapping("/api/Region")
public class RegionController {

    private final RegionService RegionService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('Region:list')")
    public void exportRegion(HttpServletResponse response, RegionQueryCriteria criteria) throws IOException {
        RegionService.download(RegionService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询地区管理")
    @ApiOperation("查询地区管理")
    @PreAuthorize("@el.check('Region:list')")
    public ResponseEntity<PageResult<Region>> queryRegion(RegionQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(RegionService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增地区管理")
    @ApiOperation("新增地区管理")
    @PreAuthorize("@el.check('Region:add')")
    public ResponseEntity<Object> createRegion(@Validated @RequestBody Region resources){
        RegionService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改地区管理")
    @ApiOperation("修改地区管理")
    @PreAuthorize("@el.check('Region:edit')")
    public ResponseEntity<Object> updateRegion(@Validated @RequestBody Region resources){
        RegionService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除地区管理")
    @ApiOperation("删除地区管理")
    @PreAuthorize("@el.check('Region:del')")
    public ResponseEntity<Object> deleteRegion(@RequestBody List<Integer> ids) {
        RegionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @GetMapping("/lazy")
    @Log("获取")
    @ApiOperation("删除地区管理")
    @PreAuthorize("@el.check('Region:del')")
    public ResponseEntity<List<Region>> queryAllRegion(@RequestParam Long parentId) {
        return new ResponseEntity<>(RegionService.queryAllRegion(parentId,true),HttpStatus.OK);
    }

    @ApiOperation("查询部门:根据ID获取同级与上级数据")
    @PostMapping("/superior")
    @PreAuthorize("@el.check('user:list','dept:list')")
        public ResponseEntity<Object> getDeptSuperior(@RequestBody List<Long> ids,
                                                      @RequestParam(defaultValue = "false") Boolean exclude) {
            return new ResponseEntity<>(RegionService.getDeptSuperior(ids, exclude),HttpStatus.OK);
        }
}